<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>类型模板管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/plugins/select2/select2.css" />
    <link rel="stylesheet" href="/plugins/select2/select2-bootstrap.css" />
    <script src="/plugins/select2/select2.min.js" type="text/javascript"></script>

</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品类型模板管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button"  class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" onclick="del()" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button>

                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                  分类模板名称：<input  >									
									<button class="btn btn-default">查询</button>                                    
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">模板ID</th>
									      <th class="sorting">分类模板名称</th>
									      <th class="sorting">关联品牌</th>
									      <th class="sorting">关联规格</th>									     
									      <th class="sorting">扩展属性</th>													     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
								  <!--数据列表/-->
			                      <tbody id="getInfo">

			                      </tbody>
			                  </table>

							<!-- 数据表格 /-->
							<form id="page">
								<a href="javascript:toPage(1)">首页</a>
								<a href="javascript:toPage(2)">上一页</a>
								第<input type="text" id="pageNum" size="1px" readonly="readonly">页/
								共<input type="text" id="pages" size="1px" readonly="readonly">页/
								共<input type="text" id="total" size="1px" readonly="readonly">条
								<a href="javascript:toPage(3)">下一页</a>
								<a href="javascript:toPage(4)">末页</a>
							</form>
							 
                        </div>
                        <!-- 数据表格 /-->
                        
                        
                        
                        
                     </div>


<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品类型模板编辑</h3>
		</div>
		<div class="modal-body">
			
			<table class="table table-bordered table-striped"  width="800px">
		      	<tr>
		      		<td>商品类型</td>
		      		<td><input  class="form-control" placeholder="商品类型" id="name">  </td>
		      	</tr>			   
		      	<tr>
		      		<td>关联品牌</td>
		      		<td>
						<select id="brandselect" style="width: 100%" multiple></select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>关联规格</td>
		      		<td>
						<select id="specselect" style="width: 100%" multiple></select>
					</td>
		      	</tr>	
		      	
		      	<tr>
		      		<td>扩展属性</td>
		      		<td>
		      			<div class="btn-group">
                             <button type="button" class="btn btn-default" title="新增扩展属性" onclick="insertinput()"><i class="fa fa-file-o"></i> 新增扩展属性</button>
                                                                    
                        </div>
						<table class="table table-bordered table-striped"  width="800px">
							<thead>
								<tr>
								    <td><input type="checkbox" class="icheckbox_square-blue"></td>
									<td>属性名称</td>									
									<td>操作</td>
								</tr>
							</thead>
							<tbody id="up">
								<tr>
									<td><input type="checkbox" class="icheckbox_square-blue" ></td>
									<td><input class="form-control" placeholder="属性名称" ></td>
									<td><button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
								</tr>
							</tbody>
						</table>	
           
		      		</td>
		      	</tr>	
		      	      
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" onclick="add()" data-dismiss="modal" aria-hidden="true">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>
  <!-- 修改窗口 -->
  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel2">商品类型模板修改</h3>
			  </div>
			  <div class="modal-body">
				  <table class="table table-bordered table-striped"  width="800px" id="">

					  <tr id="update"></tr>

					  <tr>
					      <td>关联品牌</td>
					 	  <td>
						  		<select id="ubrandselect" style="width: 100%" multiple></select>
					 	  </td>
					  </tr>
					  <tr>
						  <td>关联规格</td>
						  <td>
							   <select id="uspecselect" style="width: 100%" multiple></select>
						  </td>
					  </tr>
					  <tr>
						  <td>扩展属性</td>
						  <td>
							  <div class="btn-group">
								  <button type="button" class="btn btn-default" title="新增扩展属性" onclick="updateinput()"><i class="fa fa-file-o"></i> 新增扩展属性</button>

							  </div>
							  <table class="table table-bordered table-striped"  width="800px">
								  <thead>
								  <tr>
									  <td><input type="checkbox" class="icheckbox_square-blue"></td>
									  <td>属性名称</td>
									  <td>操作</td>
								  </tr>
								  </thead>
								  <tbody id="upp">

								  </tbody>
							  </table>

						  </td>
					  </tr>
				  </table>
			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" onclick="update()" data-dismiss="modal" aria-hidden="true">修改</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
</body>

</html>
<script>
		$(function () {
		getAll();
		})

		function getAll(num) {
		$.ajax({
			url: "/type/getInfo",
			dataType: "json",
			data: {pageNum: num},
			type: "get",
			async: false,
			success: function (r) {
				if (r.code == "1") {

					var data = r.data.rows;
					var html = '';

					$(data).each(function (i, e) {
						//关联品牌
						var specIds= e.specIds;
						var specList = JSON.parse(specIds);
						var texts = [];
						$(specList).each(function(i,e){
							texts.push(e.text)
						})

						//关联规格
						var brandIds= e.brandIds;
						var brandList = JSON.parse(brandIds)
						var btext = [];
						$(brandList).each(function (i, e) {
							btext.push(e.text)
						})

						//拓展属性
						var customAttributeItems = e.customAttributeItems;
						var clist = JSON.parse(customAttributeItems);
						var ctext = [];
						$(clist).each(function (i, e) {
							ctext.push(e.text)
						})

						html += '<tr>'
						html +=	'<td><input type="checkbox" value="'+e.id+'" name="check"></td>'
						html +=	'<td>'+ e.id +'</td>'
						html +=	'<td>'+e.name+'</td>'
						html +=	'<td>'+btext+'</td>'
						html +=	'<td>'+texts+'</td>'
						html +=	'<td>'+ctext+'</td>'
						html +=	'<td className="text-center">'
						html +=	'<button type="button" class="btn bg-olive btn-xs" data-toggle="modal" onclick="toupdate('+e.id+')" data-target="#updateModal" >修改</button>'
						html +=	'</td>'
						html +=	'</tr>'
					});
					$("#getInfo").html(html);
					//分页
					$("#pageNum").val(r.data.pageNum)
					$("#pages").val(r.data.pages)
					$("#total").val(r.data.total)
				}
			}
		})
		}

		//分页
		function toPage(id) {
		var num = parseInt($("#pageNum").val())
		var pages = parseInt($("#pages").val())
		var p = num;
		if (id == 1) {
			if (num <= 1) {
				alert("已经是首页");
				return;
			}
			p = 1;
			$("#pageNum").val(p);
		}
		if (id == 2) {
			if (num == 1) {
				alert("已经是首页")
				return;
			}
			p = num - 1;
			$("#pageNum").val(num - 1);
		}
		if (id == 3) {
			if (num == pages) {
				alert("已经是尾页")
				return;
			}
			p = num + 1;
			$("#pageNum").val(num + 1);
		}
		if (id == 4) {
			if (num == pages) {
				alert("已经是尾页")
				return;
			}
			p = pages
			$("#pageNum").val(pages);
		}
		getAll(p);
	}

		//搜索增加里的数据
		$.ajax({
			url :"/brand/slelectAll",
			type : "POST",
			dataType : "json",
			data : {},
			async :false,
			success:function(data){
				var  html = '';
				$(data).each(function(i,e) {
					html += '<option value="' + e.id + '">'+e.name + '</option>'
					$("#brandselect").html(html);
					$("#brandselect").select2();
				})
			}
		})
		$.ajax({
		url :"/spec/selectAll",
		type : "POST",
		dataType : "json",
		data : {},
		async :false,
		success:function(data){
			var  html = '';
			$(data).each(function(i,e) {
				html += '<option value="' + e.id + '">'+e.specName + '</option>'
				$("#specselect").html(html);
				$("#specselect").select2();
			})
		}
	})

		//增加
		function add(){
			//获取到select2选中下拉框的值
			var brandList = $("#brandselect").select2('data');
			var specList = $("#specselect").select2('data');
			var brandarr = [];
			var specarr = [];
			$(specList).each(function(i,e) {
				var spec = {id : e.id,text : e.text};
				specarr.push(spec);
			})
			$(brandList).each(function(i,e) {
				var brand = {id : e.id,text : e.text};
				brandarr.push(brand);
			})
			//扩展属性
			var trs = $("#up").find('tr');
			var arr = [];
			$(trs).each(function(i,e) {
				var tds = $(e).find('td');
				var TbSpecificationOption = {text:$(tds[1]).find('input').val()}
				arr.push(TbSpecificationOption)
			})
			var s = JSON.stringify(specarr);
			var b = JSON.stringify(brandarr);
			var c = JSON.stringify(arr);

			$.ajax({
				url :"/type/add",
				type : "POST",
				dataType : "json",
				data : {name:$("#name").val(),specIds:s,brandIds:b,customAttributeItems:c},
				async :false,
				success:function(data){
					if (data.code == 1){
						alert(data.message);
						window.location.reload();
					}
				},
				error:function (data) {
					alert(data.message)
				}
			})
		}


		//增加里面添加input框
		function insertinput(){
			var html ='';
			html +='<tr>\n' +
					'<td><input type="checkbox" class="icheckbox_square-blue" ></td>' +
					'<td><input class="form-control" placeholder="属性名称" ></td>' +
					'<td><button type="button" onclick="delinput(this)" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>' +
					'</tr>'
			$("#up").append(html);
		}
		function updateinput(){
			var html ='';
			html +='<tr>\n' +
					'<td><input type="checkbox" class="icheckbox_square-blue" ></td>' +
					'<td><input class="form-control" placeholder="属性名称" ></td>' +
					'<td><button type="button" onclick="delinput(this)" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>' +
					'</tr>'
			$("#upp").append(html);
		}

		//添加里面删除
		function delinput(is){
			var tr = is.parentNode.parentNode; //parentNode :获取父节点
			tr.parentNode.removeChild(tr);		//removeChild :删除子节点tr
		}

		//删除
		function del(){
			if(confirm("确定删除吗？")) {
				var boxs = document.getElementsByName("check");  //根据名字去获取值
				var ids = "";
				for (var i = 0; i < boxs.length; i++) {
					if (boxs[i].checked) {
						ids += boxs[i].value + ","
					}
				}
				ids = ids.substring(0, ids.length - 1);
				$.ajax({
					url: "/type/del?ids=" + ids,
					data: {},
					dataType: "json",
					type: "post",
					async: false,
					success: function (result) {
						if (result.code == 1) {
							alert(result.message);
							window.location.reload();
						}
					},
					error: function (result) {
						alert(result.message);
					}
				})
			}
		}

		//回显
		function toupdate(id){
			$.ajax({
				url :"/type/toupdate/"+id,
				type : "POST",
				dataType : "json",
				data : {},
				async :false,
				success:function(data){
					if (data.code ==1){
						var a = '';
						var d = data.data;
						//商品类型
						a +=  '<td>商品类型</td>'
						a +=  '\<td><input  class="form-control" type="hidden" value="'+d.id+'" id="id1">  '
						a +=  '<input  class="form-control" placeholder="商品类型" value="'+d.name+'" id="name1"></td>'
						$("#update").html(a);

						//关联品牌
						var brandIds = d.brandIds;
						var brandList = JSON.parse(brandIds)
						var branderr = [];
						$(brandList).each(function (i, e) {
							branderr.push(e.id)
						})
						console.log(branderr)
						aaa();
						//先讲id集合放入下拉框中,然后trigger(触发)change事件
						$("#ubrandselect").val(branderr).trigger("change")

						//关联规格
						var specIds = d.specIds;
						var specList = JSON.parse(specIds)
						var specarr = [];
						$(specList).each(function(i,e) {
							specarr.push(e.id)
						})
						bbb();
						//先讲id集合放入下拉框中,然后trigger(触发)change事件
						$("#uspecselect").val(specarr).trigger("change")

						//扩展属性
						var customAttributeItems = d.customAttributeItems;
						var cus = JSON.parse(customAttributeItems);
						var cerr = [];
						$(cus).each(function(i,e) {
							cerr.push(e.text)
						})
						console.log(cerr);
						var to = '';
						$(cerr).each(function(i,e){
							to += '<tr>'
							to += '<td><input type="checkbox" className="icheckbox_square-blue"></td>'
							to += '<td><input className="form-control" placeholder="属性名称" value="'+e+'"></td>'
							to += '<td>'
							to += '<button type="button" onclick="delinput(this)" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button>'
							to += '</td>'
							to += '</tr>'
						})
						$("#upp").html(to);

					}
				}
			})
		}

		//修改
		function update(){
			//获取到select2选中下拉框的值
			var brandList = $("#ubrandselect").select2('data');
			var specList = $("#uspecselect").select2('data');
			var brandarr = [];
			var specarr = [];
			$(specList).each(function(i,e) {
				var spec = {id : e.id,text : e.text};
				specarr.push(spec);
			})
			$(brandList).each(function(i,e) {
				var brand = {id : e.id,text : e.text};
				brandarr.push(brand);
			})
			//扩展属性
			var trs = $("#upp").find('tr');
			var arr = [];
			$(trs).each(function(i,e) {
				var tds = $(e).find('td');
				var TbSpecificationOption = {text:$(tds[1]).find('input').val()}
				arr.push(TbSpecificationOption)
			})
			var s = JSON.stringify(specarr);
			var b = JSON.stringify(brandarr);
			var c = JSON.stringify(arr);
			$.ajax({
				url :"/type/update",
				type : "POST",
				dataType : "json",
				data : {id:$("#id1").val(),name:$("#name1").val(),specIds:s,brandIds:b,customAttributeItems:c},
				async :false,
				success:function(data){
						if(data.code == 1){
							alert(data.message);
						}
				},
				error :function(data){
					alert(data.message);
				},
			})
		}

		function bbb(){
			$.ajax({
				url :"/spec/selectAll",
				type : "POST",
				dataType : "json",
				data : {},
				async :false,
				success:function(data){
					var  html = '';
					$(data).each(function(i,e) {
						html += '<option value="' + e.id + '">'+e.specName + '</option>'
						$("#uspecselect").html(html);
						$("#uspecselect").select2();
					})
				}
			})
		}
		function aaa(){
			$.ajax({
				url :"/brand/slelectAll",
				type : "POST",
				dataType : "json",
				data : {},
				async :false,
				success:function(data){
					var  html = '';
					$(data).each(function(i,e) {
						html += '<option value="' + e.id + '">'+e.name + '</option>'
						$("#ubrandselect").html(html);
						$("#ubrandselect").select2();
					})
				}
			})
		}
</script>